package com.tencent.mid.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Proxy;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.tencent.mid.api.MidEntity;
import com.tencent.mid.api.MidService;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPInputStream;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.http.HttpHost;
import org.json.JSONArray;
import org.json.JSONObject;

public class Util
{
  public static byte[] StringToBytes(String paramString)
  {
    if (paramString == null)
      return null;
    try
    {
      byte[] arrayOfByte = paramString.getBytes("UTF-8");
      return arrayOfByte;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    return paramString.getBytes();
  }

  public static String bytesToString(byte[] paramArrayOfByte)
  {
    if (paramArrayOfByte == null)
      return null;
    try
    {
      String str = new String(paramArrayOfByte, "UTF-8");
      return str;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    return new String(paramArrayOfByte);
  }

  public static boolean checkPermission(Context paramContext, String paramString)
  {
    int j = 0;
    try
    {
      int i = paramContext.getPackageManager().checkPermission(paramString, paramContext.getPackageName());
      if (i == 0)
        j = 1;
      return j;
    }
    catch (Throwable paramContext)
    {
      Log.e("MID", "checkPermission error", paramContext);
    }
    return false;
  }

  public static void clear(Context paramContext)
  {
    if (paramContext == null)
      return;
    com.tencent.mid.b.g.a(paramContext).c();
  }

  public static String decode(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    do
    {
      return str;
      str = paramString;
    }
    while (Build.VERSION.SDK_INT < 8);
    try
    {
      str = new String(g.b(Base64.decode(paramString.getBytes("UTF-8"), 0)), "UTF-8").trim().replace("\t", "").replace("\n", "").replace("\r", "");
      return str;
    }
    catch (Throwable localThrowable)
    {
      Log.e("MID", "decode error", localThrowable);
    }
    return paramString;
  }

  public static byte[] deocdeGZipContent(byte[] paramArrayOfByte)
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
    GZIPInputStream localGZIPInputStream = new GZIPInputStream(localByteArrayInputStream);
    byte[] arrayOfByte = new byte[4096];
    paramArrayOfByte = new ByteArrayOutputStream(paramArrayOfByte.length * 2);
    while (true)
    {
      int i = localGZIPInputStream.read(arrayOfByte);
      if (i == -1)
        break;
      paramArrayOfByte.write(arrayOfByte, 0, i);
    }
    arrayOfByte = paramArrayOfByte.toByteArray();
    localByteArrayInputStream.close();
    localGZIPInputStream.close();
    paramArrayOfByte.close();
    return arrayOfByte;
  }

  public static String encode(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    do
    {
      return str;
      str = paramString;
    }
    while (Build.VERSION.SDK_INT < 8);
    try
    {
      str = new String(Base64.encode(g.a(paramString.getBytes("UTF-8")), 0), "UTF-8").trim().replace("\t", "").replace("\n", "").replace("\r", "");
      return str;
    }
    catch (Throwable localThrowable)
    {
      Log.w("MID", "encode error", localThrowable);
    }
    return paramString;
  }

  public static boolean equal(MidEntity paramMidEntity1, MidEntity paramMidEntity2)
  {
    if ((paramMidEntity1 != null) && (paramMidEntity2 != null))
      if (paramMidEntity1.compairTo(paramMidEntity2) != 0);
    do
    {
      return true;
      return false;
    }
    while ((paramMidEntity1 == null) && (paramMidEntity2 == null));
    return false;
  }

  public static String getDeviceModel()
  {
    return Build.MODEL;
  }

  public static DisplayMetrics getDisplayMetrics(Context paramContext)
  {
    DisplayMetrics localDisplayMetrics = new DisplayMetrics();
    ((WindowManager)paramContext.getApplicationContext().getSystemService("window")).getDefaultDisplay().getMetrics(localDisplayMetrics);
    return localDisplayMetrics;
  }

  public static String getExternalStorageInfo(Context paramContext)
  {
    try
    {
      if (checkPermission(paramContext, "android.permission.WRITE_EXTERNAL_STORAGE"))
      {
        paramContext = Environment.getExternalStorageState();
        if ((paramContext == null) || (!(paramContext.equals("mounted"))))
          break label130;
        paramContext = Environment.getExternalStorageDirectory().getPath();
        if (paramContext == null)
          break label130;
        paramContext = new StatFs(paramContext);
        long l1 = paramContext.getBlockCount() * paramContext.getBlockSize() / 1000000L;
        long l2 = paramContext.getAvailableBlocks();
        l2 = paramContext.getBlockSize() * l2 / 1000000L;
        return String.valueOf(l2) + "/" + String.valueOf(l1);
      }
      Log.e("MID", "can not get the permission of android.permission.WRITE_EXTERNAL_STORAGE");
      return null;
    }
    catch (Throwable paramContext)
    {
      Log.e("MID", "", paramContext);
    }
    label130: return null;
  }

  public static byte[] getHMAC(String paramString1, String paramString2)
  {
    try
    {
      paramString1 = new SecretKeySpec(paramString1.getBytes(), "hmacmd5");
      Mac localMac = Mac.getInstance("HmacSHA1");
      localMac.init(paramString1);
      localMac.update(paramString2.getBytes());
      paramString1 = localMac.doFinal();
      return paramString1;
    }
    catch (java.lang.Exception paramString1)
    {
      logWarn(paramString1);
    }
    return null;
  }

  public static HttpHost getHttpProxy()
  {
    if (Proxy.getDefaultHost() != null)
      return new HttpHost(Proxy.getDefaultHost(), Proxy.getDefaultPort());
    return null;
  }

  public static HttpHost getHttpProxy(Context paramContext)
  {
    if (paramContext == null)
      return null;
    do
    {
      try
      {
        if (paramContext.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", paramContext.getPackageName()) != 0)
          return null;
        paramContext = ((ConnectivityManager)paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
        if (paramContext == null)
          return null;
        if ((paramContext.getTypeName() != null) && (paramContext.getTypeName().equalsIgnoreCase("WIFI")))
          return null;
        paramContext = paramContext.getExtraInfo();
        logInfo("network type:" + paramContext);
        if (paramContext == null)
          return null;
        if ((!(paramContext.equals("cmwap"))) && (!(paramContext.equals("3gwap"))) && (!(paramContext.equals("uniwap"))))
          break label150;
        paramContext = new HttpHost("10.0.0.172", 80);
        return paramContext;
      }
      catch (Throwable paramContext)
      {
        logWarn(paramContext);
        return null;
      }
      if (paramContext.equals("ctwap"))
        label150: return new HttpHost("10.0.0.200", 80);
      paramContext = Proxy.getDefaultHost();
    }
    while ((paramContext == null) || (paramContext.trim().length() <= 0));
    paramContext = new HttpHost(paramContext, Proxy.getDefaultPort());
    return paramContext;
  }

  public static String getHttpUrl()
  {
    return "http://pingmid.qq.com:80/";
  }

  public static String getImei(Context paramContext)
  {
    try
    {
      if (checkPermission(paramContext, "android.permission.READ_PHONE_STATE"))
      {
        paramContext = ((TelephonyManager)paramContext.getSystemService("phone")).getDeviceId();
        if (paramContext != null)
          return paramContext;
      }
      logInfo("Could not get permission of android.permission.READ_PHONE_STATE");
      return "";
    }
    catch (Throwable paramContext)
    {
      logWarn(paramContext);
    }
  }

  public static String getLinkedWay(Context paramContext)
  {
    Object localObject;
    try
    {
      if ((checkPermission(paramContext, "android.permission.INTERNET")) && (checkPermission(paramContext, "android.permission.ACCESS_NETWORK_STATE")))
      {
        localObject = ((ConnectivityManager)paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
        if ((localObject == null) || (!(((NetworkInfo)localObject).isConnected())))
          break label122;
        paramContext = ((NetworkInfo)localObject).getTypeName();
        localObject = ((NetworkInfo)localObject).getExtraInfo();
        if (paramContext == null)
          break label122;
        if (paramContext.equalsIgnoreCase("WIFI"))
          return "WIFI";
        if (!(paramContext.equalsIgnoreCase("MOBILE")))
          break label116;
        if (localObject == null)
          break label112;
        return localObject;
      }
      Log.e("MID", "can not get the permission of android.permission.ACCESS_WIFI_STATE");
      return null;
    }
    catch (Throwable paramContext)
    {
      Log.e("MID", "", paramContext);
      return null;
    }
    label112: return "MOBILE";
    if (localObject != null)
    {
      label116: return localObject;
      label122: paramContext = null;
    }
    return ((String)paramContext);
  }

  public static MidEntity getNewerMidEntity(MidEntity paramMidEntity1, MidEntity paramMidEntity2)
  {
    if ((paramMidEntity1 != null) && (paramMidEntity2 != null))
      if (paramMidEntity1.compairTo(paramMidEntity2) < 0);
    do
    {
      return paramMidEntity1;
      return paramMidEntity2;
    }
    while (paramMidEntity1 != null);
    if (paramMidEntity2 != null)
      return paramMidEntity2;
    return null;
  }

  public static String getRemoteUrlIp(String paramString)
  {
    try
    {
      paramString = InetAddress.getByName(new URL(paramString).getHost());
      if (paramString == null)
        break label31;
      paramString = paramString.getHostAddress();
      return paramString;
    }
    catch (Throwable paramString)
    {
      paramString.printStackTrace();
    }
    label31: return null;
  }

  public static String getSimOperator(Context paramContext)
  {
    try
    {
      if (checkPermission(paramContext, "android.permission.READ_PHONE_STATE"))
      {
        paramContext = (TelephonyManager)paramContext.getSystemService("phone");
        if (paramContext == null)
          break label56;
        paramContext = paramContext.getSimOperator();
        break label58:
      }
      Log.e("MID", "Could not get permission of android.permission.READ_PHONE_STATE");
      return null;
    }
    catch (Throwable paramContext)
    {
      Log.e("MID", "", paramContext);
      return null;
    }
    label56: paramContext = null;
    label58: return paramContext;
  }

  public static Integer getTelephonyNetworkType(Context paramContext)
  {
    try
    {
      paramContext = (TelephonyManager)paramContext.getSystemService("phone");
      if (paramContext == null)
        break label26;
      int i = paramContext.getNetworkType();
      return Integer.valueOf(i);
    }
    catch (Throwable paramContext)
    {
    }
    label26: return null;
  }

  public static String getWiFiBBSID(Context paramContext)
  {
    try
    {
      paramContext = getWifiInfo(paramContext);
      if (paramContext == null)
        break label21;
      paramContext = paramContext.getBSSID();
      return paramContext;
    }
    catch (Throwable paramContext)
    {
      logWarn(paramContext);
    }
    label21: return null;
  }

  public static String getWiFiSSID(Context paramContext)
  {
    try
    {
      paramContext = getWifiInfo(paramContext);
      if (paramContext == null)
        break label21;
      paramContext = paramContext.getSSID();
      return paramContext;
    }
    catch (Throwable paramContext)
    {
      logWarn(paramContext);
    }
    label21: return null;
  }

  public static WifiInfo getWifiInfo(Context paramContext)
  {
    if (checkPermission(paramContext, "android.permission.ACCESS_WIFI_STATE"))
    {
      paramContext = (WifiManager)paramContext.getApplicationContext().getSystemService("wifi");
      if (paramContext != null)
        return paramContext.getConnectionInfo();
    }
    return null;
  }

  public static String getWifiMacAddress(Context paramContext)
  {
    if (checkPermission(paramContext, "android.permission.ACCESS_WIFI_STATE"))
      try
      {
        paramContext = (WifiManager)paramContext.getSystemService("wifi");
        if (paramContext == null)
          return "";
        paramContext = paramContext.getConnectionInfo().getMacAddress();
        return paramContext;
      }
      catch (java.lang.Exception paramContext)
      {
        logInfo("get wifi address error" + paramContext);
        return "";
      }
    logInfo("Could not get permission of android.permission.ACCESS_WIFI_STATE");
    return "";
  }

  public static JSONArray getWifiTopN(Context paramContext, int paramInt)
  {
    JSONArray localJSONArray;
    try
    {
      if ((checkPermission(paramContext, "android.permission.INTERNET")) && (checkPermission(paramContext, "android.permission.ACCESS_NETWORK_STATE")))
      {
        paramContext = (WifiManager)paramContext.getSystemService("wifi");
        if (paramContext != null)
        {
          paramContext = paramContext.getScanResults();
          if ((paramContext != null) && (paramContext.size() > 0))
          {
            Collections.sort(paramContext, new j());
            localJSONArray = new JSONArray();
            int i = 0;
            while (true)
            {
              if ((i >= paramContext.size()) || (i >= paramInt))
                break label168;
              ScanResult localScanResult = (ScanResult)paramContext.get(i);
              JSONObject localJSONObject = new JSONObject();
              localJSONObject.put("bs", localScanResult.BSSID);
              localJSONObject.put("ss", localScanResult.SSID);
              localJSONArray.put(localJSONObject);
              i += 1;
            }
          }
        }
      }
      logInfo("can not get the permisson of android.permission.INTERNET");
      return null;
    }
    catch (Throwable paramContext)
    {
      logWarn(paramContext);
    }
    label168: return localJSONArray;
  }

  public static boolean isMidValid(String paramString)
  {
    return ((paramString == null) || (paramString.trim().length() < 40));
  }

  public static boolean isNetworkAvailable(Context paramContext)
  {
    try
    {
      if ((!(checkPermission(paramContext, "android.permission.INTERNET"))) || (!(checkPermission(paramContext, "android.permission.ACCESS_NETWORK_STATE"))))
        break label75;
      paramContext = (ConnectivityManager)paramContext.getSystemService("connectivity");
      if (paramContext == null)
        break label75;
      paramContext = paramContext.getActiveNetworkInfo();
      if ((paramContext != null) && (paramContext.isAvailable()))
        return true;
      Log.w("MID", "Network error");
      return false;
    }
    catch (Throwable paramContext)
    {
      Log.e("MID", "isNetworkAvailable error", paramContext);
    }
    label75: return false;
  }

  public static boolean isStringValid(String paramString)
  {
    return ((paramString == null) || (paramString.trim().length() == 0));
  }

  public static boolean isWifiNet(Context paramContext)
  {
    paramContext = ((ConnectivityManager)paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
    return ((paramContext == null) || (paramContext.getTypeName() == null) || (!(paramContext.getTypeName().equalsIgnoreCase("WIFI"))));
  }

  public static void jsonPut(JSONObject paramJSONObject, String paramString1, String paramString2)
  {
    if (!(isStringValid(paramString2)))
      return;
    paramJSONObject.put(paramString1, paramString2);
  }

  public static void logInfo(String paramString)
  {
    if (!(MidService.isEnableDebug()))
      return;
    Log.i("MID", paramString);
  }

  public static void logWarn(Throwable paramThrowable)
  {
    if (!(MidService.isEnableDebug()))
      return;
    Log.w("MID", paramThrowable);
  }

  public static String md5(String paramString)
  {
    try
    {
      Object localObject = MessageDigest.getInstance("MD5");
      ((MessageDigest)localObject).update(paramString.getBytes("UTF-8"));
      localObject = ((MessageDigest)localObject).digest();
      StringBuffer localStringBuffer = new StringBuffer();
      int i = 0;
      while (i < localObject.length)
      {
        localStringBuffer.append(localObject[i]);
        i += 1;
      }
      localObject = localStringBuffer.toString();
      return localObject;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      logWarn(localNoSuchAlgorithmException);
      return paramString;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      logWarn(localUnsupportedEncodingException);
    }
    return ((String)paramString);
  }

  public static void updateIfLocalInvalid(Context paramContext, String paramString)
  {
    if (!(isMidValid(paramString)))
      return;
    MidEntity localMidEntity = new MidEntity();
    localMidEntity.setImei(getImei(paramContext));
    localMidEntity.setMac(getWifiMacAddress(paramContext));
    localMidEntity.setMid(paramString);
    com.tencent.mid.b.g.a(paramContext).a(localMidEntity);
  }
}